<?php

class productModel extends Model {

    public function getNewProduct() {
        $sql = "SELECT *	
                    FROM product 
                    ORDER BY timeStart DESC
                    LIMIT 5;
                    ";

        $this->_setSql($sql);
        $NewProduct = $this->getAll();

        if (empty($NewProduct)) {
            return false;
        }
        return $NewProduct;
    }

    public function getOldProduct() {
        $sql = "SELECT * FROM product WHERE timeEnd >= CURDATE() 
Order by timeStart DESC
Limit 5; ";

        $this->_setSql($sql);
        $oldProducts = $this->getAll();

        if (empty($oldProducts)) {
            return false;
        }
        return $oldProducts;
    }

    public function getHotProduct() {
        $sql = "SELECT p.idProduct, p.name,p.timeEnd, count(idTransaction) count FROM product p, transaction t WHERE timeEnd > NOW() and p.idProduct =t.idProduct
group by p.idProduct 
order by count DESC
limit 5; ";

        $this->_setSql($sql);
        $hotProducts = $this->getAll();

        if (empty($hotProducts)) {
            return false;
        }
        return $hotProducts;
    }

    /*
     * Get Product by Id
     * @param: id
     * @return: row of product
     */

    public function getProductbyId($id) {
        $sql = "SELECT *	
                    FROM product 
                    WHERE idProduct = '$id'
                    ";

        $this->_setSql($sql);
        $product = $this->getRow();
        if (empty($product)) {
            return false;
        }
        else
            return $product;
    }

    public function getHighestPrice($id) {
        $sql = "SELECT MAX(price) as value
                    FROM transaction 
                    WHERE idProduct = '$id'
                    ";

        $this->_setSql($sql);
        $product = $this->getRow();
        if (empty($product)) {
            return false;
        }
        else
            return $product;
    }

    public function getSearch($sql) {
        $this->_setSql($sql);

        $listProduct = $this->getAll();

        if (empty($listProduct)) {
            return false;
        }
        return $listProduct;
    }

    public function getCount($sql) {
        $this->_setSql($sql);

        $listProduct = $this->getAll();

        if (empty($listProduct)) {
            return 0;
        }
        return count($listProduct);
    }

}

?>